package Shell;

public class FileSystem {

	private static Directory root = new Directory("/");
	private static Directory home = root;
	private static FileUtil fileUtil = new FileUtil();

	public static void cd() {
		home = root;
	}

	public static void cd(String path) {
		Directory dir = null;

		if(path.charAt(0) != '/')
			dir = fileUtil.getDir(root, path.substring(1));
		else dir = fileUtil.getDir(home, path);

		if(dir != null)
			home = dir;
		else System.err.println("Invalid path");
	}

	public static void ls() {
		fileUtil.showChildNames(home);
	}

	public static void ls(String path) {

		Directory dir = null;

		if(path.charAt(0) == '/')
			dir = fileUtil.getDir(root, path.substring(1));
		else dir = fileUtil.getDir(home, path);

		if(dir != null) {
			fileUtil.showChildNames(dir);
		}
		else {
			System.err.println("Invalid path");
		}
	}

	public static void mkdir(String path) {
		
		String newDirName = path.substring(path.lastIndexOf('/')+1);
		path = path.substring(0, path.lastIndexOf('/')-1);
		
		Directory dir = null;

		if(path.charAt(0) == '/')
			dir = fileUtil.getDir(root, path.substring(1));
		else dir = fileUtil.getDir(home, path);

		if(dir == null) {
			System.err.println("Invalid path");
		}
		else {
			Directory newDir = new Directory(newDirName);
			newDir.setParent(dir);
			
			dir.getDirList().add(newDir);
		}
	}
	
	public static void rmdir(String path) {
		Directory dir = null;

		if(path.charAt(0) == '/')
			dir = fileUtil.getDir(root, path.substring(1));
		else dir = fileUtil.getDir(home, path);
		
		if(dir != null) {
			Directory parent = dir.getParent();
			parent.getDirList().remove(dir);
		}
		else {
			System.err.println("Invalid path");
		}
	}
	
	public static void cfile(String filePath) {
		
		Directory dir = null;
		
		if(filePath.indexOf('/') != -1) {
			dir = home;
		}
		else {
			String fileName = filePath.substring(filePath.lastIndexOf('/')+1);
			String path = filePath.substring(0, filePath.lastIndexOf('/')-1);
			
			if(path.charAt(0) == '/')
				dir = fileUtil.getDir(root, path.substring(1));
			else dir = fileUtil.getDir(home, path);
		}
		
		if(dir != null) {
			File file = new File(filePath);
			dir.getFileList().add(file);
		}
		else {
			System.err.println("Invalid path");
		}
	}
	
	public static void wfile(String filePath, String content) {
		Directory dir = null;
		String fileName = "";
		
		if(filePath.indexOf('/') != -1) {
			dir = home;
			fileName = filePath;
		}
		else {
			fileName = filePath.substring(filePath.lastIndexOf('/')+1);
			String path = filePath.substring(0, filePath.lastIndexOf('/')-1);
			
			if(path.charAt(0) == '/')
				dir = fileUtil.getDir(root, path.substring(1));
			else dir = fileUtil.getDir(home, path);
		}
		
		if(dir != null) {
			File file = null;
			for(File childFile : dir.getFileList()) {
				if(childFile.getFileName().equals(fileName))
					file = childFile;
			}
			
			if(file != null)
				file.setFileContent(content);
			else System.err.println("File not found");
		}
		else {
			System.err.println("Invalid path");
		}
	}
	
	public static void empty(String filePath) {
		wfile(filePath, "");
	}
}
